import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 主页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '主页', icon: 'dashboard' }
    }]
  },
  // 商铺管理
  {
    path: '/shop',
    component: Layout,
    redirect: '/shop/shoplist',
    name: 'Shop',
    meta: { title: '商铺管理', icon: 'shop' },
    children: [
      {
        path: 'shoplist',
        name: 'ShopList',
        component: () => import('@/views/shop/shoplist/index'),
        meta: { title: '商铺列表', icon: 'shoplist' }
      },
      {
        path: 'productlist',
        name: 'ProductList',
        component: () => import('@/views/shop/productlist/index'),
        meta: { title: '商品列表', icon: 'commodity' }
      },
      {
        path: 'setupshopaudit',
        name: 'SetUpShopAudit',
        component: () => import('@/views/shop/setupshopaudit/index'),
        meta: { title: '开店审核', icon: 'setupshop' }
      },
      {
        path: 'shopsetting',
        name: 'ShopSetting',
        component: () => import('@/views/shop/shopsetting/index'),
        meta: { title: '店铺设置', icon: 'shopsetting' }
      }
    ]
  },
  // 商圈管理
  {
    path: '/businessdistrict',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'BusinessDistrict',
        component: () => import('@/views/businessdistrict/index'),
        meta: { title: '商圈管理', icon: 'businessdistrict' }
      }
    ]
  },
  // 订单管理
  {
    path: '/order',
    component: Layout,
    redirect: '/order/orderlist',
    name: 'Order',
    meta: {
      title: '订单管理',
      icon: 'order'
    },
    children: [
      {
        path: 'orderlist',
        component: () => import('@/views/order/orderlist/index'), // Parent router-view
        name: 'OrderList',
        meta: { title: '订单列表', icon: 'orderlist' }
      },
      {
        path: 'orderstatistics',
        component: () => import('@/views/order/orderstatistics/index'),
        name: 'OrderStatistics',
        meta: { title: '订单统计', icon: 'orderstatistics' }
      }
    ]
  },
  // 素材管理
  {
    path: '/material',
    component: Layout,
    redirect: '/material/officialmaterial',
    name: 'material',
    meta: { title: '素材管理', icon: 'material' },
    children: [
      {
        path: 'officialmaterial',
        name: 'OfficialMaterial',
        component: () => import('@/views/material/officialmaterial/index'),
        meta: { title: '官方素材', icon: 'officialmaterial' }
      },
      {
        path: 'materiallist',
        name: 'MaterialList',
        component: () => import('@/views/material/materiallist/index'),
        meta: { title: '素材列表', icon: 'materiallist' }
      }
    ]
  },
  // 优惠券管理
  {
    path: '/discountcoupon',
    component: Layout,
    redirect: '/discountcoupon/generaldiscountcoupon',
    name: 'DiscountCoupon',
    meta: { title: '优惠券管理', icon: 'nested' },
    children: [
      {
        path: 'generaldiscountcoupon',
        name: 'GeneralDiscountCoupon',
        component: () => import('@/views/discountcoupon/generaldiscountcoupon/index'),
        meta: { title: '通用优惠券', icon: 'form' }
      },
      {
        path: 'shopdiscountcoupon',
        name: 'ShopDiscountCoupon',
        component: () => import('@/views/discountcoupon/shopdiscountcoupon/index'),
        meta: { title: '店铺优惠券', icon: 'form' }
      }
    ]
  },
  // 财务管理
  {
    path: '/finance',
    component: Layout,
    redirect: '/finance/IncomeAndExpenses',
    name: 'Finance',
    meta: { title: '财务管理', icon: 'nested' },
    children: [
      {
        path: 'incomeandexpenses',
        name: 'IncomeAndExpenses',
        component: () => import('@/views/finance/incomeandexpenses/index'),
        meta: { title: '平台收支明细', icon: 'form' }
      },
      {
        path: 'withdrawdeposit',
        name: 'WithdrawDeposit',
        component: () => import('@/views/finance/withdrawdeposit/index'),
        meta: { title: '提现管理', icon: 'form' }
      }
    ]
  },
  // 系统设置
  {
    path: '/system',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'System',
        component: () => import('@/views/system/index'),
        meta: { title: '系统设置', icon: 'form' }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
